package manage.model;

import m.common.model.FieldMeta;
import m.common.model.Model;
import m.common.model.TableMeta;
import m.common.model.type.FieldType;
import m.common.model.util.ModelQueryList;
import m.common.model.util.QueryCondition;
import m.common.model.util.QueryOrder;
import m.system.cache.CacheUtil;
import m.system.cache.FlushCacheList;
import m.system.util.ObjectUtil;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@TableMeta(name="os_module_info",description="模块信息表")
public class ModuleInfo extends Model implements FlushCacheList,IMenuName {
	private static final long serialVersionUID = 1L;

	@FieldMeta(name="name",type=FieldType.STRING,length=20,description="名称")
	private String name;
	@FieldMeta(name="url_path",type=FieldType.STRING,length=200,description="地址")
	private String urlPath;
	@FieldMeta(name="ico_style",type=FieldType.STRING,length=100,description="图标样式")
	private String icoStyle;
	@FieldMeta(name="sort",type=FieldType.INT,description="排序")
	private Integer sort;
	@FieldMeta(name="type",type=FieldType.STRING,length=20,description="类型",dictOption = "A:普通菜单,B:右侧菜单")
	private String type;
	@FieldMeta(name="is_public",type=FieldType.STRING, description="是否公共Y/N")
	private String isPublic;
	@FieldMeta(name="is_input",type=FieldType.STRING, description="是否录入Y/N")
	private String isInput;
	public String getName() {
		return name;
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	public void setName(String name) {
		this.name = name;
	}

	public Integer getSort() {
		return sort;
	}

	public String getIsInput() {
		return isInput;
	}

	public void setIsInput(String isInput) {
		this.isInput = isInput;
	}

	public void setSort(Integer sort) {
		this.sort = sort;
	}

	public String getIcoStyle() {
		return icoStyle;
	}

	public void setIcoStyle(String icoStyle) {
		this.icoStyle = icoStyle;
	}

	public String getUrlPath() {
		return urlPath;
	}

	public void setUrlPath(String urlPath) {
		this.urlPath = urlPath;
	}

	public String getIsPublic() {
		return isPublic;
	}

	public void setIsPublic(String isPublic) {
		this.isPublic = isPublic;
	}

	protected static QueryCondition getAdminCondition(String oidField, Map<String,String> map, AdminLogin admin, OrgGroupView orgGroup){
		if(null==map) map=new HashMap<>();
		return QueryCondition.or(
			QueryCondition.eq("isPublic", "Y"),
			QueryCondition.in("oid",
				ModelQueryList.instance(GroupMenuLink.class,
					new String[]{oidField}, null,
					QueryCondition.or(
						QueryCondition.and(
							QueryCondition.eq("adminGroup.oid",admin.getAdminGroup().getOid()),
							QueryCondition.eq("adminGroup.status","0")
						),
						QueryCondition.in("adminGroup.oid",
							ModelQueryList.instance(AdminGroupLink.class,
								new String[] {"adminGroup.oid"}, null,
								QueryCondition.and(
									QueryCondition.eq("admin.oid", admin.getOid()),
									QueryCondition.eq("adminGroup.status","0"),
									QueryCondition.eq("type","B"),
									QueryCondition.or(
										QueryCondition.isEmpty("orgGroup.oid"),
										QueryCondition.eq("orgGroup.oid",orgGroup.getOid()),
										QueryCondition.eq("orgGroup.parent.oid",orgGroup.getOid())
									)
								)
							)
						),
						QueryCondition.eq("adminGroup.oid",orgGroup.getOid()),
						QueryCondition.eq("adminGroup.oid", ObjectUtil.notEmptyOid(orgGroup.getDept())?orgGroup.getDept().getOid():"")
					),
					map
				)
			)
		);
	}
	@Override
	public List<ModuleInfo> getCacheList(String key) throws Exception {
		//key用逗号分开 1:adminOid 管理员 ,2:orgGroupOid 登录机构
		String[] arr=key.split(",");
		AdminLogin admin= CacheUtil.get(AdminLogin.class,arr[0]);
		OrgGroupView org=arr.length>1?CacheUtil.get(OrgGroupView.class,arr[1]):new OrgGroupView();
		return ModelQueryList.getModelList(ModuleInfo.class,
			new String[]{"oid","name","type","urlPath","icoStyle"},
			null,
			getAdminCondition("module.oid",null,admin,org),
			QueryOrder.asc("sort")
		);
	}
}
